﻿using Studio77.GuessTheWordsWin8.Core;
using Studio77.GuessTheWordsWin8.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// “用户控件”项模板在 http://go.microsoft.com/fwlink/?LinkId=234236 上提供

namespace Studio77.GuessTheWordsWin8.Views
{
    public sealed partial class CharacterBlock : UserControl
    {
        public CharacterBlock()
        {
            this.InitializeComponent();
        }

        public bool IsCompleted = false;

        // 当前此block的状态
        public int CurrentType = 0;

        public int Row;
        public int Column;
        public int HorizonWordIndex;
        public int VerticalWordIndex;

        public String HorizontalTip = "";
        public String VerticalTip = "";

        /// <summary>
        /// 横向拼音
        /// </summary>
        public String AimPy;
        
        /// <summary>
        /// 竖向拼音
        /// </summary>
        public String AimPy2;

        /// <summary>
        /// 目标中文
        /// </summary>
        public String AimChinese;

        public String CurrentPy = "";


        /// <summary>
        /// 设置成为完成状态
        /// </summary>
        public void setComplete()
        {
            IsCompleted = true;
            this.txtContent.Text = AimChinese;

            // 设置为完成状态
            StageData sd = StageManager.Instance.getCurrentStageData();
            MatrixCharInfo mci = new MatrixCharInfo();
            mci.Row = Row;
            mci.Column = Column;
            mci.IsComplete = true;
            sd.CharInfoList[Row * 10 + Column] = mci;

            //StageManager.Instance.saveStageDatas();
        }


        /// <summary>
        /// 设置横向目标
        /// </summary>
        /// <param name="py"></param>
        /// <param name="chinese"></param>
        public void setAimHorizon(String py, String chinese)
        {
            AimChinese = chinese;
            AimPy = py;
        }

        /// <summary>
        /// 设置竖向目标
        /// </summary>
        /// <param name="py"></param>
        /// <param name="chinese"></param>
        public void setAimVertical(String py, String chinese)
        {
            AimChinese = chinese;
            AimPy2 = py;
        }

        /// <summary>
        /// 切换类型
        /// </summary>
        /// <param name="type">0-normal, 1-nochar, 2-selected, 3-sense</param>
        public void changeBackgroundType(int type)
        {
            //只有状态变化，且当前状态不为无字符状态才允许
            if (CurrentType != type)
            {
                this.gridSelect.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                this.StoryboardSelect.Stop();
                switch (type)
                {
                    case 0:
                        bdrBack.Background = new SolidColorBrush(Color.FromArgb(255, 238, 238, 238));
                        break;
                    case 1:
                        bdrBack.Background = new SolidColorBrush(Color.FromArgb(255, 151, 151, 151));
                        break;
                    case 2:
                        bdrBack.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
                        this.gridSelect.Visibility = Windows.UI.Xaml.Visibility.Visible;
                        this.StoryboardSelect.RepeatBehavior = RepeatBehavior.Forever;
                        this.StoryboardSelect.Begin();
                        break;
                    case 3:
                        bdrBack.Background = new SolidColorBrush(Color.FromArgb(255, 255, 113, 113));
                        break;
                    case 4:
                        bdrBack.Background = new SolidColorBrush(Color.FromArgb(255, 138, 255, 126));
                        break;
                }
                if (type == BlockType.NoChar)
                {
                    txtContent.Visibility = Visibility.Collapsed;
                }
                else
                {
                    txtContent.Visibility = Visibility.Visible;
                }
                CurrentType = type;
            }
        }



        /// <summary>
        /// 显示的字符
        /// </summary>
        /// <param name="str"></param>
        public void setShowChar(String str)
        {
            // 如果已经完成了该词条，无需更新
            if (!IsCompleted)
            {
                this.txtContent.Text = str;
                CurrentPy = str;
                // 设置为完成状态
                StageData sd = StageManager.Instance.getCurrentStageData();
                MatrixCharInfo mci = new MatrixCharInfo();
                mci.Row = Row;
                mci.Column = Column;
                mci.IsComplete = false;
                mci.CharStr = str;
                sd.CharInfoList[Row * 10 + Column] = mci;

                //StageManager.Instance.saveStageDatas();
            }
        }

        /// <summary>
        /// 是否填写正确
        /// </summary>
        /// <returns></returns>
        public bool isCorrectPy()
        {
            if (IsCompleted)
            {
                return true;
            }
            else
            {
                return CurrentPy == AimPy || CurrentPy == AimPy2;
            }
        }
    }


    public class BlockType
    {
        public const int Normal = 0;
        public const int NoChar = 1;
        public const int Selected = 2;
        public const int SenseHorizon = 3;
        public const int SenseVertical = 4;
    }
}
